﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.XHX
{
    [Description("【销售订单表单插件.获取即时库存】"), HotUpdate]
    public class JustStockGet : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
        }
        public override void BeforeSubmit(BeforeSubmitEventArgs e)
        {
            base.BeforeSubmit(e);
            for (int i = 0; i < this.Model.GetEntryRowCount("FSaleOrderEntry"); i++)
            {

                DynamicObjectCollection treeEntity = this.Model.DataObject["SaleOrderEntry"] as DynamicObjectCollection;
                DynamicObject material = treeEntity[i]["MaterialId"] as DynamicObject;
                string materialNo = material["Number"].ToString();
                DynamicObject StockOrgId = treeEntity[i]["STOCKORGID"] as DynamicObject;
                if (StockOrgId == null)
                {
                    throw new KDBusinessException("第" + i.ToString(), "库存组织不为空！");
                }
                string org = StockOrgId["Id"].ToString();
                DynamicObject Stock = Model.DataObject["F_JHKJ_BASE"] as DynamicObject;
                if (Stock == null)
                {
                    throw new KDBusinessException("单据头仓库不为空", "仓库不为空！");
                }
                string stockId = Stock["Id"].ToString();
                var assistantField = (AssistantField)this.View.BillBusinessInfo.GetField("F_JHKJ_Assistant");

                DynamicObject qudao = Model.DataObject["F_JHKJ_Assistant"] as DynamicObject;
                string cangweinumber = qudao["FNumber"].ToString();
                //if (StockID == null)
                //{
                //    throw new KDBusinessException("第" + i.ToString(), "仓位不为空！");
                //}
                Model.SetItemValueByNumber("F_JHKJ_Base1", cangweinumber, i);

                DynamicObject cangwei = Model.DataObject["F_JHKJ_Base1"] as DynamicObject;

                string FSOSTOCKLOCALID = cangwei["Id"].ToString();
                decimal qty = 0;
                string sql = string.Format(@"/*dialect*/ SELECT sum(s.FBASEQTY)qty  FROM T_STK_INVENTORY s 
	            INNER JOIN dbo.T_BD_MATERIAL a ON s.FMATERIALID=a.FMATERIALID 
	            INNER JOIN dbo.t_BD_Stock o ON o.FUSEORGID=a.FUSEORGID
	            WHERE a.FMATERIALID=s.FMATERIALID
	            and a.FUSEORGID='{0}'
	            and a.FNUMBER='{1}'
	            and a.FUSEORGID=s.FSTOCKORGID
	            AND o.FStockId={2}
	            GROUP by a.FUSEORGID", org, materialNo, stockId);

                DataSet ds = DBUtils.ExecuteDataSet(Context, sql);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    qty = Convert.ToDecimal(ds.Tables[0].Rows[0]["qty"]);
                }
                this.Model.SetValue("F_CURRENTQTY", qty, i);

                this.View.UpdateView("F_CURRENTQTY", i);

            }
        }
    }
}
